package com.itcm.purchase.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itcm.purchase.common.utils.DoubleCalcUtil;
import com.itcm.purchase.entity.DailyReport;
import com.itcm.purchase.mapper.DailyReportMapper;
import com.itcm.purchase.service.DailyReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 每日报表 服务实现类
 * </p>
 *
 * @author cm_fighting
 * @since 2020-07-29
 */
@Service
@Slf4j
public class DailyReportServiceImpl extends ServiceImpl<DailyReportMapper, DailyReport> implements DailyReportService {

    @Override
    public boolean saveReport(DailyReport dailyReport) {
        System.out.println("saveReport");
        System.out.println(dailyReport.getId());
        System.out.println(dailyReport.getReportDate());
        System.out.println(dailyReport.getType());
        Date reportDate = dailyReport.getReportDate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formatDate = sdf.format(reportDate);
        if (null == dailyReport.getId() && null != dailyReport.getReportDate() && null != dailyReport.getType()) {
            List<DailyReport> list = getByDateAndType(formatDate, dailyReport.getType());
            System.out.println(list);
            System.out.println(list.size());
            if (list.size() > 0) {
                throw new RuntimeException("已存在，新增失败");
            }
        }

        // 合计
        // 胚芽 1#
        if (null != dailyReport.getPyoneFrom() && null != dailyReport.getPyoneTo()) {
//            dailyReport.setPyoneTotal(dailyReport.getPyoneTo() - dailyReport.getPyoneFrom());
            dailyReport.setPyoneTotal(DoubleCalcUtil.sub(dailyReport.getPyoneTo(), dailyReport.getPyoneFrom()));
        }
        // 胚芽 2#
        if (null != dailyReport.getPytwoFrom() && null != dailyReport.getPytwoTo()) {
//            dailyReport.setPytwoTotal(dailyReport.getPytwoTo() - dailyReport.getPytwoFrom());
            dailyReport.setPytwoTotal(DoubleCalcUtil.sub(dailyReport.getPytwoTo(), dailyReport.getPytwoFrom()));
        }
        // 喷浆皮 1#
        if (null != dailyReport.getPjponeFrom() && null != dailyReport.getPjponeTo()) {
//            dailyReport.setPjponeTotal(dailyReport.getPjponeTo() - dailyReport.getPjponeFrom());
            dailyReport.setPjponeTotal(DoubleCalcUtil.sub(dailyReport.getPjponeTo(), dailyReport.getPjponeFrom()));
        }
        // 喷浆皮 2#
        if (null != dailyReport.getPjptwoFrom() && null != dailyReport.getPjptwoTo()) {
//            dailyReport.setPjptwoTotal(dailyReport.getPjptwoTo() - dailyReport.getPjptwoFrom());
            dailyReport.setPjptwoTotal(DoubleCalcUtil.sub(dailyReport.getPjptwoTo(), dailyReport.getPjptwoFrom()));
        }

        // 外总表
        if (null != dailyReport.getEleWzbFrom() && null != dailyReport.getEleWzbTo()) {
//            dailyReport.setEleWzbTotal((dailyReport.getEleWzbTo() - dailyReport.getEleWzbFrom()) * 120);
            dailyReport.setEleWzbTotal(DoubleCalcUtil.mul(DoubleCalcUtil.sub(dailyReport.getEleWzbTo(), dailyReport.getEleWzbFrom()), (double) 120));
        }

        // 内大表
        if (null != dailyReport.getEleNdbFrom() && null != dailyReport.getEleNdbTo()) {
//            dailyReport.setEleNdbTotal((dailyReport.getEleNdbTo() - dailyReport.getEleNdbFrom()) * 120);
            dailyReport.setEleNdbTotal(DoubleCalcUtil.mul(DoubleCalcUtil.sub(dailyReport.getEleNdbTo(), dailyReport.getEleNdbFrom()), (double) 120));
        }

        // 包装间表
        if (null != dailyReport.getEleBzjFrom() && null != dailyReport.getEleBzjTo()) {
//            dailyReport.setEleBzjTotal((dailyReport.getEleBzjTo() - dailyReport.getEleBzjFrom()) * 120);
            dailyReport.setEleBzjTotal(DoubleCalcUtil.mul(DoubleCalcUtil.sub(dailyReport.getEleBzjTo(), dailyReport.getEleBzjFrom()), (double) 120));
        }

        // 分表 1#
        if (null != dailyReport.getEleOneFrom() && null != dailyReport.getEleOneTo()) {
//            dailyReport.setEleOneTotal((dailyReport.getEleOneTo() - dailyReport.getEleOneFrom()) * 120);
            dailyReport.setEleOneTotal(DoubleCalcUtil.mul(DoubleCalcUtil.sub(dailyReport.getEleOneTo(), dailyReport.getEleOneFrom()), (double) 120));
        }

        // 分表 2#
        if (null != dailyReport.getEleTwoFrom() && null != dailyReport.getEleTwoTo()) {
//            dailyReport.setEleTwoTotal((dailyReport.getEleTwoTo() - dailyReport.getEleTwoFrom()) * 120);
            dailyReport.setEleTwoTotal(DoubleCalcUtil.mul(DoubleCalcUtil.sub(dailyReport.getEleTwoTo(), dailyReport.getEleTwoFrom()), (double) 120));
        }

        // 天然气
        if (null != dailyReport.getGasFrom() && null != dailyReport.getGasTo()) {
//            dailyReport.setGasTotal(dailyReport.getGasTo() - dailyReport.getGasFrom());
            dailyReport.setGasTotal(DoubleCalcUtil.sub(dailyReport.getGasTo(), dailyReport.getGasFrom()));
        }

        // 水
        if (null != dailyReport.getWaterFrom() && null != dailyReport.getWaterTo()) {
//            dailyReport.setWaterTotal(dailyReport.getWaterTo() - dailyReport.getWaterFrom());
            dailyReport.setWaterTotal(DoubleCalcUtil.sub(dailyReport.getWaterTo(), dailyReport.getWaterFrom()));
        }

        // 饼 1#
        if (null != dailyReport.getCakeOneFrom() && null != dailyReport.getCakeOneTo()) {
//            dailyReport.setCakeOneTotal(dailyReport.getCakeOneTo() - dailyReport.getCakeOneFrom());
            dailyReport.setCakeOneTotal(DoubleCalcUtil.sub(dailyReport.getCakeOneTo(), dailyReport.getCakeOneFrom()));
        }

        // 饼 2#
        if (null != dailyReport.getCakeTwoFrom() && null != dailyReport.getCakeTwoTo()) {
//            dailyReport.setCakeTwoTotal(dailyReport.getCakeTwoTo() - dailyReport.getCakeTwoFrom());
            dailyReport.setCakeTwoTotal(DoubleCalcUtil.sub(dailyReport.getCakeTwoTo(), dailyReport.getCakeTwoFrom()));
        }

        // 油 流量计
        if (null != dailyReport.getOilFlowmeterFrom() && null != dailyReport.getOilFlowmeterTo()) {
//            dailyReport.setOilFlowmeterTotal(dailyReport.getOilFlowmeterTo() - dailyReport.getOilFlowmeterFrom());
            dailyReport.setOilFlowmeterTotal(DoubleCalcUtil.sub(dailyReport.getOilFlowmeterTo(), dailyReport.getOilFlowmeterFrom()));
        }

        // 油 罐数
        if (null != dailyReport.getOilTankNumFrom() && null != dailyReport.getOilTankNumTo()) {
            dailyReport.setOilTankNumTotal(dailyReport.getOilTankNumTo() - dailyReport.getOilTankNumFrom());

        }

        // 总计
        // 胚芽
        if (null != dailyReport.getPyoneTotal() && null != dailyReport.getPytwoTotal()) {
//            dailyReport.setPyTotal(dailyReport.getPyoneTotal() + dailyReport.getPytwoTotal());
            dailyReport.setPyTotal(DoubleCalcUtil.add(dailyReport.getPyoneTotal(), dailyReport.getPytwoTotal()));
        }

        // 喷浆皮
        if (null != dailyReport.getPjponeTotal() && null != dailyReport.getPjptwoTotal()) {
//            dailyReport.setPjpTotal(dailyReport.getPjponeTotal() + dailyReport.getPjptwoTotal());
            dailyReport.setPjpTotal(DoubleCalcUtil.add(dailyReport.getPjponeTotal(), dailyReport.getPjptwoTotal()));
        }
        // 电
        if (null != dailyReport.getEleWzbTotal() && null != dailyReport.getEleNdbTotal() && null != dailyReport.getEleBzjTotal() && null != dailyReport.getEleOneTotal() && null != dailyReport.getEleTwoTotal()) {
//            dailyReport.setEleTotal(dailyReport.getEleWzbTotal() + dailyReport.getEleNdbTotal() + dailyReport.getEleBzjTotal() + dailyReport.getEleOneTotal() + dailyReport.getEleTwoTotal());
            Double add1 = DoubleCalcUtil.add(dailyReport.getEleWzbTotal(), dailyReport.getEleNdbTotal());
            Double add2 = DoubleCalcUtil.add(add1, dailyReport.getEleBzjTotal());
            Double add3 = DoubleCalcUtil.add(add2, dailyReport.getEleOneTotal());
            Double add4 = DoubleCalcUtil.add(add3, dailyReport.getEleTwoTotal());
            dailyReport.setEleTotal(add4);
        }
        // 饼
        if (null != dailyReport.getCakeOneTotal() && null != dailyReport.getCakeTwoTotal()) {
//            dailyReport.setCakeTotal(dailyReport.getCakeOneTotal() + dailyReport.getCakeTwoTotal());
            dailyReport.setCakeTotal(DoubleCalcUtil.add(dailyReport.getCakeOneTotal(), dailyReport.getCakeTwoTotal()));
        }

        return this.saveOrUpdate(dailyReport);
    }

    @Override
    public List<DailyReport> getByDate(String date) {
        QueryWrapper<DailyReport> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("report_date", date);
        return this.list(queryWrapper);
    }

    public List<DailyReport> getByDateAndType(String date, Integer type) {
        QueryWrapper<DailyReport> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("report_date", date);
        queryWrapper.eq("type", type);
        return this.list(queryWrapper);
    }
}